﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Security;
using System.Web.Security;
using System.Text;
using System.Net.Mail;
using System.Collections.Specialized;
using System.Configuration;
using LizzabethBrownRealEstate.Models;

namespace LizzabethBrownRealEstate
{
    public class GlobalApplication : System.Web.HttpApplication
    {
        public static string extensionHelper = ".ashx";

        //public static void RegisterRoutes(RouteCollection routes)
        //{
        //    // Note: Change the URL to "{controller}.mvc/{action}/{id}" to enable
        //    //       automatic support on IIS6 and IIS7 classic mode

        //    routes.Add(new Route("{controller}" + extensionHelper + "/{action}/{id}", new MvcRouteHandler())
        //    {
        //        Defaults = new RouteValueDictionary(new { action = "Index", id = "" }),
        //    });

        //    routes.Add(new Route("Default.aspx", new MvcRouteHandler())
        //    {
        //        Defaults = new RouteValueDictionary(new { controller = "Home", action = "Index", id = "" }),
        //    });
        //}

        //protected void Application_Start(object sender, EventArgs e)
        //{
        //    RegisterRoutes(RouteTable.Routes);
        //}

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}" + extensionHelper + "/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Exception ex = Server.GetLastError().GetBaseException();
            if (ex is SecurityException)
            {
                Response.Redirect("/Home" + extensionHelper + "/Index");
            }
            else 
            {
                
                //reportErrors();
                //Response.Redirect("/Home" + extensionHelper + "/Error");
            }
        }

        private void reportErrors()
        {

            StringBuilder serverVariables = new StringBuilder();
            serverVariables.Append("Server variables:" + Environment.NewLine + "----------" + Environment.NewLine);
            for (int i = 0; i < Request.ServerVariables.Count; i++)
            {
                serverVariables.Append(Request.ServerVariables.Keys[i] + ":\t\t" + Request.ServerVariables[i] + Environment.NewLine);
            }
            serverVariables.Append(Environment.NewLine);

            NameValueCollection section = (NameValueCollection)ConfigurationManager.GetSection("emailInfo");
            
            string body = "";
            body = "ERROR: " + Server.GetLastError().GetBaseException().Message + Environment.NewLine;
            if (Server.GetLastError().GetBaseException().InnerException != null)
            {
                body += "Inner Exception: " + Server.GetLastError().GetBaseException().InnerException.ToString() + Environment.NewLine + Environment.NewLine;
            }

            body += "Variables: " + Environment.NewLine + serverVariables.ToString();

            MailAddress to = new MailAddress("vinbrown@cox.net");
            MailMessage message = new MailMessage();
            message.To.Add(to);
            message.IsBodyHtml = false;
            message.Subject = "LizzabethBrown.com Error";
            message.Body = body;
            SmtpClient smtp = new SmtpClient();

            if (!body.Contains("RssPortal"))
            {
                // smtp.Send(message);
            }
        }

        private string getQueryString()
        {
            StringBuilder queryString = new StringBuilder();
            queryString.Append("Query String:" + Environment.NewLine + "----------" + Environment.NewLine);
            for (int i = 0; i < Request.QueryString.Count; i++)
            {
                queryString.Append(Request.QueryString.Keys[i] + ":\t\t" + Request.QueryString[i] + Environment.NewLine);
            }
            queryString.Append(Environment.NewLine);
            return queryString.ToString();
        }
    }
}